package com.fg.cloud.factory.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.ProblemStatus;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.system.ProblemInteractiveEntity;
import com.fg.cloud.manager.entry.system.SystemProblemEntity;
import com.fg.cloud.manager.service.system.SystemProblemService;

@Controller
@RequestMapping("/systemPro")
public class SystemProblemController extends BaseController {

	@Autowired
	private SystemProblemService systemProblemService;
	
	/**
	 * Description:系统平台用于接收全平台所有的系统BUG类型问题反馈，可做回复处理
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @author dingjiang
	 * @date 2017年9月4日下午7:24:54
	 */
	@RequestMapping(value="/systemRecep",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void systemRecep(HttpServletRequest req, HttpServletResponse resp, Integer page, Integer rows, 
			String sort, String order){
		try {
			if (null == page) {
				page = 1;
			}
			if (null == rows) {
				rows = 10;
			}
			PageUtils<SystemProblemEntity> pageinfo = new PageUtils<SystemProblemEntity>(page, rows, sort, order);
			pageinfo.setSort("status");
			pageinfo.setOrder("desc");
			pageinfo.getCondition().put("type = ", ProblemStatus.SYSPROBLEM);
			systemProblemService.selectpage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}

	
	/**
	 * Description:系统管理员处理全平台的系统BUG类型问题反馈
	 * @param req
	 * @param resp
	 * @param id
	 * @param text
	 * @author dingjiang
	 * @date 2017年9月5日上午9:32:54
	 */
	@RequestMapping(value="/reply",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void reply(HttpServletRequest req, HttpServletResponse resp, Long id, String text){
		try {
			//验证反馈ID非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请选择问题");
				return ;
			}
			//验证反馈ID是否合法
			SystemProblemEntity systemProblemEntity = systemProblemService.find(id);
			if ( null == systemProblemEntity || ProblemStatus.BUSIPROBLEM == systemProblemEntity.getType() ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//判断问题状态
			if ( systemProblemEntity.getStatus() == ProblemStatus.PROCESSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已处理");
				return ;
			}
			if ( systemProblemEntity.getStatus() == ProblemStatus.CLOSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已关闭");
				return ;
			}
			//验证回复内容非空
			if (StringUtils.isEmpty(text)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请输入回复内容");
				return ;
			}
			//回复用户
			ProblemInteractiveEntity interactive = new ProblemInteractiveEntity();
			interactive.setType((byte) 1);
			interactive.setContent(text);
			interactive.setProId(id);
			systemProblemService.reply(interactive);
			//修改问题状态
			systemProblemEntity.setStatus(ProblemStatus.PROCESSED);
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}	
	}
	
}
